<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <!-- 标准号、标准名称 -->
      <el-form-item label="标准号" prop="standardNum">
        <el-input v-model="queryParams.standardNum" placeholder="请输入标准号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="标准名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入标准名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重 置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="标准号" align="center" prop="standardNum" />
      <el-table-column label="标准名称" align="center" prop="name" />
      <el-table-column label="标准内容" width="150" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)">标准内容</el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="updateUserName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改流程对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标准号" prop="standardNum">
              <el-input v-model="form.standardNum" placeholder="请输入标准号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入标准名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="步骤列表">
              <div class="step-header">
                <el-input v-model="stepContent" placeholder="请输入步骤内容" style="margin-bottom: 10px;">
                  <template #append>
                    <el-button @click="addStep">添加步骤</el-button>
                  </template>
                </el-input>
              </div>
              <el-table :data="form.stepList" style="width: 100%">
                <el-table-column type="index" label="序号" width="80" />
                <el-table-column prop="" label="步骤内容">
                  <template #default="scope">
                    {{ scope.row }}
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="120" align="center">
                  <template #default="scope">
                    <el-button link type="danger" @click="removeStep(scope.$index)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="open2" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">

            <el-form-item v-for="(item, index) in form.contentList" :key="index" :label="`流程${index}`">
              <el-input :value="item" placeholder="请输入流程标题" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import {
  listProcess,
  listProcessStep,
  addProcess,
  getNotice,
  delProcess,
  addNotice,
  updateNotice,
} from "@/api/backProduce/backProduce";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const stepContent = ref('');

const data = reactive({
  form: {
    standardNum: undefined,
    name: undefined,
    stepList: []
  },
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    standardNum: undefined,
    name: undefined,
  },
  rules: {
    standardNum: [
      { required: true, message: "标准号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "标准名称不能为空", trigger: "blur" }
    ]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询流程列表 */
function getList() {
  loading.value = true;
  listProcess(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    standardNum: undefined,
    name: undefined,
    status: 1,
    stepList: []
  };
  stepContent.value = '';
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.produceProcessId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加流程";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      addProcess(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const produceProcessIds = row.produceProcessId || ids.value;
  proxy.$modal
    .confirm('是否确认删除流程编号为"' + produceProcessIds + '"的数据项？')
    .then(function () {
      return delProcess(produceProcessIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}


/**修改按钮操作 */
let open2 = ref(false);
function handleDetail(row) {
  reset();
  const produceProcessId = row.produceProcessId || ids.value;
  listProcessStep(produceProcessId).then((response) => {
    form.value.contentList = response
    open2.value = true;
    title.value = "流程详情";
  });
}

// 添加步骤
function addStep() {
  if (stepContent.value.trim()) {
    form.value.stepList.push(stepContent.value.trim());
    stepContent.value = '';
  }
}

// 删除步骤
function removeStep(index) {
  form.value.stepList.splice(index, 1);
}

function getStatusType(status) {
  switch (status) {
    case 1: return 'warning'  // 待生产
    case 2: return 'info'     // 待质检
    case 3: return 'primary'  // 待入库
    case 4: return 'success'  // 已入库
    default: return 'info'
  }
}

function getStatusLabel(status) {
  switch (status) {
    case 1: return '待生产'
    case 2: return '待质检'
    case 3: return '待入库'
    case 4: return '已入库'
    default: return '未知'
  }
}

getList();
</script>

<style scoped>
.step-list {
  border: 1px solid #EBEEF5;
  border-radius: 4px;
  padding: 10px;
}

.step-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  padding: 10px;
  background: #f5f7fa;
  border-radius: 4px;
}

.step-number {
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  background: #409EFF;
  color: white;
  border-radius: 50%;
  margin-right: 10px;
  flex-shrink: 0;
}

.step-content {
  flex: 1;
  margin-right: 10px;
}

.step-actions {
  display: flex;
  gap: 5px;
}
</style>